<?php

namespace app\common\business;

use app\common\model\mysql\SysRole as SysRoleModel;
use app\common\model\mysql\SysUserRole as SysUserRoleModel;

class SysRole
{

    public $roleObj = null;
    public $userRoleObj = null;
    public function __construct()
    {
        $this->roleObj = new SysRoleModel();
        $this->userRoleObj = new SysUserRoleModel();
    }

    //查询角色列表
    public function getNormalRole($where,$field = "*",$limit,$page){
        $roleData = $this->roleObj->getNormalRoleList($where,$field,$limit,$page);
        if(!$roleData){
            return [];
        }
        $roleData = $roleData->toArray();
        return $roleData;
    }

    //查询角色列表(无分页)
    public function getNormalRoleNotPage($where = [],$field = "*"){
        $roleData = $this->roleObj->getNormalRoleNotPageList($where,$field);
        if(!$roleData){
            return [];
        }
        $roleData = $roleData->toArray();
        return $roleData;
    }

    //查询角色数据filed数组
    public function getNormalRoleFiled($where = [],$field = "role_name"){
        $roleData = $this->roleObj->getNormalRoleFiledArr($where,$field);
        if(!$roleData){
            return [];
        }
        return $roleData;
    }

    //根据角色ids数组获取数据范围数组
    public function getRoleDataScope($ids){
        $roleData = $this->roleObj->getRoleDataScopeModel($ids);
        if(!$roleData){
            return [];
        }
        return $roleData;
    }

    //添加角色
    public function addRole($data){
        //根据$role_name去数据库查询是否存在这条记录
        $postNameResult = $this->getRoleByName($data['role_name']);
        if($postNameResult){
            throw new \think\Exception("该角色名称已经存在请重新设置");
        }
        //根据$role_key去数据库查询是否存在这条记录
        $postCodeResult = $this->getRoleByKey($data['role_key']);
        if($postCodeResult){
            throw new \think\Exception("该权限字符已经存在请重新设置");
        }
        try {
            $this->roleObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->roleObj->id;
    }

    public function getRoleByName($role_name,$role_id = ''){
        $user = $this->roleObj->getRoleByNameFind($role_name,$role_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    public function getRoleByKey($role_key,$role_id = ''){
        $user = $this->roleObj->getRoleByKeyFind($role_key,$role_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getRoleById($id){
        $user = $this->roleObj->getRoleByIdFind($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        //根据$role_name去数据库查询是否存在这条记录
        $postNameResult = $this->getRoleByName($data['role_name'],$id);
        if($postNameResult){
            throw new \think\Exception("该角色名称已经存在请重新设置");
        }
        //根据$role_key去数据库查询是否存在这条记录
        $postCodeResult = $this->getRoleByKey($data['role_key'],$id);
        if($postCodeResult){
            throw new \think\Exception("该权限字符已经存在请重新设置");
        }
        return $this->roleObj->updateRoleById($id,$data);
    }

    //根据$ids数组，删除数据库记录delete
    public function DelRoleIds($ids){
        $ids = explode(',', $ids);
        //根据$ids去数据库查询是否存在这条记录
        $postResult = $this->userRoleObj->passUserRoleDelIds($ids);
        if($postResult){
            $postData = $this->getRoleById($postResult->role_id);
            throw new \think\Exception($postData['role_name']."已分配,不能删除");
        }
        $result = $this->roleObj->DelRoleIds($ids);
        return $result;
    }

    /**
     * 根据主键id更新角色状态
     * @param $id
     * @param $data
     * @return bool
     */
    public function changeStatusRole($roleId,$status){
        $roleId = intval($roleId);
        if(empty($roleId)){
            return false;
        }
        //根据$roleId去数据库查询这条记录
        $postNameResult = $this->getRoleById($roleId);
        if($postNameResult){
            if($postNameResult['role_key'] == "admin"){
                throw new \think\Exception("不允许操作超级管理员角色");
            }
        }
        return $this->roleObj->changeStatusRoleModel($roleId,$status);
    }
}
